<?php
/*
 * @Descripttion: 
 * @Author: Twoknives
 * @Date: 2021-07-16 21:26:57
 * @LastEditTime: 2021-12-04 23:52:14
 * @FilePath: \njz\app\admin\controller\manageuser\User.php
 */

namespace app\admin\controller\manageuser;

use app\admin\controller\anx\Adv;
use app\api\model\Agent;
use app\api\model\User as ModelUser;
use app\common\controller\AdminController;
use dh2y\qrcode\QRcode;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="用户管理")
 */
class user extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\User();
        
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
                foreach ($list as $k => $v){
                   
                    //推荐人
                    // $distributor = Db::name("distributor")->where("id",$v["superior_fid"])->find();
                    // if(!empty($distributor)){
                    //     $v['referrer_user'] = $distributor['name'];
                    // }else{
                    //     $v['referrer_user'] = "";
                    // }
                    
                    //达人
                    $service_technician = Db::name("service_technician")->where("id",$v["service_technician_id"])->field("service_name,phone")->find();
                    if(!empty($service_technician)){
                        $v["service_technician_name"] = $service_technician["service_name"].$service_technician["phone"];
                    }else{
                        $v["service_technician_name"] = "";
                    }
                    

                    $list[$k] = $v;
                }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    public function qwe(){
        $coupon = Db::name('coupon')->where('release_or_not',1)->order('id desc')->field('id,title as name,title as text')->select()->toArray();
        return json($coupon);
    }
 
    /**
    * @NodeAnotation(title="编辑")
    */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            if(empty($post['xuanzhong'])){
                $this->error('优惠券不能为空');
            }
            $xuanzhong = explode(',',$post['xuanzhong']);
            $xuanzhong = array_filter($xuanzhong);
          
            foreach($xuanzhong as $k=>$v){
                $data = [
                    'coupon_id'=>$v,
                    'uid'=>$post['xid'],
                    'add_time'=>date('Y-m-d H:i:s',time())
                ];
                Db::name('coupon_user')->insert($data);
                //优惠券订阅消息通知
                // $arr = Db::name('coupon')->where('id',$v)->field('title,money,endtime')->find();
                // $arr['tishi'] = "可前往个人中心-优惠券界面查看"; 
                //$user = Db::name('user')->where('id',$post['xid'])->field('wx_openid')->find();
                //$openid = $user['wx_openid'];
                //$adv->real_authentication($openid,$arr,2);
            }
            $result = [
                'code'=>1,
                'msg' =>'发放成功'
            ];
            return $result;
        }else{
            $post = $this->request->param('id');
            $this->assign('xid',$post);
        }
        return $this->fetch();
    }

    /**
    * @NodeAnotation(title="成为分销商")
    */
    public function adopt_url()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            $user = Db::name("user")->where('id', $post['id'])->where("is_check",1)->find();
            if (!empty($user)) {
                $this->error('已是分销商,不能重复操作');
            }
            //生成H5二维码
            $qrcode = $this->createQrcode($post['id']);
            Db::name("user")->where('id', $post['id'])->update(["is_check"=>1,"qrcode"=>$qrcode]);
            $this->success('操作成功', '');
        }
    }
     //生成H5二维码
     public function createQrcode($mid)
     {
         $code = new QRcode();
         //$res = $code->png('https://www.baidu.com/',false, 6)->logo('logo.png')->entry();
         $res = $code->png('https://pw.xianmeta.com/peiwan/#/pages/index/index?fid='.$mid,false, 10)->entry();
         return $res;
     }


      //绑定达人 
      public function binding_experts()
      {
          if ($this->request->isAjax()) {
              $post = $this->request->param();
              if(empty($post["phone"])){
                  $this->error("手机号不能为空");
              }
              $user = Db::name("user")->where("id",$post["id"])->where("service_technician_id",">",0)->find();
              if(!empty($user)){
                $this->error("已绑定，请先解绑");
              }
              $flag = Db::name("service_technician")->where("phone",$post["phone"])->find();
              if(empty($flag)){
                    $this->error("手机号错误，达人不存在");
              }
              //查询达人是否已被别人绑定
              $asdf = Db::name("user")->where("service_technician_id",$flag["id"])->find();
              if(!empty($asdf)){
                $this->error("此达人已被绑定，请更换手机号");
              }
              Db::name("service_technician")->where("id",$flag["id"])->update(["uid"=>$post["id"]]);
              $this->model->where("id",$post["id"])->update(["service_technician_id"=>$flag["id"]]);
              $this->success('绑定成功');
          }else{
              return $this->fetch();
          }
      }

      //解绑达人 
      public function unbinding_experts()
      {
          if ($this->request->isAjax()) {
              $post = $this->request->param();
              $user = Db::name("user")->where("id",$post["id"])->where("service_technician_id",">",0)->find();
              if(empty($user)){
                $this->error("未绑定");
              }
              Db::name("service_technician")->where("id",$user["service_technician_id"])->update(["uid"=>0]);
              $this->model->where("id",$post["id"])->update(["service_technician_id"=>0]);
              $this->success('解绑成功');
          }
      }

 
 

    
}